let arr = [0, 0, 0, 0]
//验证手机号码是否存在------------------------
$("#username").blur(function () {
	//前端验证
	let regObj = /^[a-zA-Z]{1,20}$/;
	if (!regObj.test(this.value)) {
		$("#msg01").html("格式不正确").css({
			"color": "red"
		});
		arr[0] = 0;
	}
	else {
		//后端验证（存在性验证）
		ajax2013useobjandpromise({
			url: "php/checkUser.php",
			params: `username=${$("#username").val()}`
		}).then(function(msg){
			if (msg == "1") {
				$("#msg01").html("用户名存在，不能注册").css({
					"color": "red"
				})
				arr[0] = 0;
			}
			else {
				$("#msg01").html("用户名不存在，可以注册").css({
					"color": "blue"
				})
				arr[0] = 1;
			}
		})
	}
})
//input聚焦失焦事件
let inputList = document.querySelectorAll("input")
inputList.forEach(item => {
	item.onfocus = function () {
		this.style.border = "1px solid red"
		this.style.borderRadius = "8px"
	}
	item.onblur = function () {
		this.style.border = "none"
	}
})
//验证密码格式是否正确
$("#userpass").blur(function () {
	let regExp = /^\w{6,18}$/;
	if (!regExp.test(this.value)) {
		$("#msg03").html("密码格式不正确，请输入6-18位组合密码").css({
			"color": "red"
		});
		arr[1] = 0;
	}
	else {
		$("#msg03").html("√").css({
			"color": "blue"
		})
		arr[1] = 1;
	}
	checkPassword()
})
//确认密码
$("#password02").blur(function () {
	checkPassword()
})
//验证两次密码是否一致
function checkPassword() {
	if ($("#password02").val() != $("#userpass").val()) {
		$("#msg04").html("两次密码不一致").css({
			"color": "red"
		});
		arr[2] = 0;
	}
	else {
		$("#msg04").html("√").css({
			"color": "blue"
		})
		arr[2] = 1;
	}
}
//验证码---arr【3】-----------------------------------
function code() {
	var arrList = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
	var result = "";
	for (var i = 0; i < 4; i++) {
		if (parseInt(Math.random() * 2)) {
			result += arrList[parseInt(Math.random() * arrList.length)].toUpperCase()
		}
		else {
			result += arrList[parseInt(Math.random() * arrList.length)]
		}
	}
	return result;
}
let sCode = document.getElementById("sCode")
let yzm = document.getElementById("yzm")
let yCode = document.getElementById("yCode")
sCode.innerHTML = code()
//看不清
yzm.onclick = function () {
	sCode.innerHTML = code()
}

//验证码input #yCode
yCode.onkeyup = function () {
	var yCodeReg = /^[A-z0-9]{4}$/
	if (yCodeReg.test(this.value) && this.value == sCode.innerHTML) {
		this.className = "pass"
		$("#msg02").html("√").css({
			"color": "blue"
		})
	}
	else {
		this.className = "error"
		$("#msg02").html("验证码有误，请重新输入").css({
			"color": "red"
		})
	}
}
//复选框
$("#check").click(function () {
	if ($("#check").css({ "checked": "checked" })) {
		arr[3] = 1;
	}
})
//注册------------------------------------
$("#psd").click(function () {
	if (!arr.every(item => { return item === 1 })) {
		$("#msg06").html("格式不正确").css({
			"color": "red"
		})
	}
	else {
		ajax2013useobjandpromise({
			url: "php/addUser.php",
			method: "post",
			params: `username=${$("#username").val()}&userpass=${$("#userpass").val()}`
		}).then(function (msg) {
			if (msg == 0) {
				$("#msg06").html("注册失败，用户名已存在").css({
					"color": "red", "display": "block"
				})
				arr[0] = 0;
			}
			else {
				$("#msg06").html("注册成功!请  <a href='weidian_login.html'>登录</a>").css({
					"color": "blue", "display": "block"
				})
				arr[0] = 1;
			}
		})
	}
})
